package com.example.interviewassistant;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView;
import androidx.appcompat.app.AppCompatActivity;

public class ApiConfigActivity extends AppCompatActivity {

    private EditText etApiUrl;
    private EditText etApiKey;
    private Spinner spinnerModelType;
    private Button btnSaveConfig;

    // 模型类型列表
    private String[] modelTypes = {
        ApiConfigManager.MODEL_GPT35,
        ApiConfigManager.MODEL_GPT4,
        ApiConfigManager.MODEL_DEEPSEEK
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_api_config);

        etApiUrl = findViewById(R.id.etApiUrl);
        etApiKey = findViewById(R.id.etApiKey);
        spinnerModelType = findViewById(R.id.spinnerModelType);
        btnSaveConfig = findViewById(R.id.btnSaveConfig);

        // 设置模型类型下拉菜单
        setupModelTypeSpinner();

        // 加载已保存的配置
        ApiConfig apiConfig = ApiConfigManager.loadConfig(this);
        if (apiConfig != null) {
            etApiUrl.setText(apiConfig.getApiUrl());
            etApiKey.setText(apiConfig.getApiKey());
            
            // 设置模型类型选择
            setSelectedModelType(apiConfig.getModelType());
        } else {
            // 如果没有保存的配置，默认选择DeepSeek模型并设置默认URL
            etApiUrl.setText(ApiConfigManager.API_URL_DEEPSEEK);
            setSelectedModelType(ApiConfigManager.MODEL_DEEPSEEK);
        }
        
        // 添加下拉菜单选择监听器
        spinnerModelType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedModel = modelTypes[position];
                String currentUrl = etApiUrl.getText().toString().trim();
                
                // 只有在URL为空或者与默认URL匹配时才自动替换
                if (currentUrl.isEmpty() || 
                    currentUrl.equals(ApiConfigManager.API_URL_DEEPSEEK) || 
                    currentUrl.equals(ApiConfigManager.API_URL_OPENAI)) {
                    
                    String recommendedUrl = ApiConfigManager.getDefaultApiUrlForModel(selectedModel);
                    etApiUrl.setText(recommendedUrl);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 不做任何处理
            }
        });

        btnSaveConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String apiUrl = etApiUrl.getText().toString().trim();
                String apiKey = etApiKey.getText().toString().trim();
                String modelType = spinnerModelType.getSelectedItem().toString();

                if (apiUrl.isEmpty() || apiKey.isEmpty()) {
                    Toast.makeText(ApiConfigActivity.this, "API地址和密钥不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                ApiConfig newConfig = new ApiConfig(apiUrl, apiKey, modelType);
                ApiConfigManager.saveConfig(ApiConfigActivity.this, newConfig);
                Toast.makeText(ApiConfigActivity.this, "配置已保存", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
    
    private void setupModelTypeSpinner() {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
                android.R.layout.simple_spinner_item, modelTypes);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerModelType.setAdapter(adapter);
    }
    
    private void setSelectedModelType(String modelType) {
        for (int i = 0; i < modelTypes.length; i++) {
            if (modelTypes[i].equals(modelType)) {
                spinnerModelType.setSelection(i);
                break;
            }
        }
    }
} 